package com.jec.module.sysmonitor.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.jec.module.business.service.BusinessService;
import com.jec.module.sysmanage.service.SysLogService;
import com.jec.module.sysmonitor.service.DeviceStateService;
import com.jec.module.sysmonitor.service.NetWorkMonitorService;
import com.jec.module.sysmonitor.service.NetWorkStateService;
import com.jec.utils.Constants;
import com.jec.utils.Response;
import com.jec.utils.tool.StringHelper;

/**
 * Created by jeremyliu on 5/11/16.
 */
@Controller
@RequestMapping(value = "/network")
public class NetWorkMonitorController {

	@Autowired
    private NetWorkMonitorService netWorkMonitorService;

    @Autowired
    private NetWorkStateService netWorkStateService;
    
    @Autowired
    private BusinessService businessService;
    
    @Autowired
    private DeviceStateService deviceStateService;

    @Autowired
    private SysLogService sysLogService;

    @RequestMapping(method = RequestMethod.POST, value = "netunit/create")
    public @ResponseBody
    Response createNetUnit(@RequestParam(value="name") String name,
                           @RequestParam(value="ip") String ip,
                           @RequestParam(value="location") String location,
                           @RequestParam(value="netId") int netId,
                           @RequestParam(value="slot", required = false) Integer slot){
        Response res = Response.Builder().status(Response.STATUS_PARAM_ERROR);
        if(name.equals("") || name.length() > 60){
            res.message("网元名称不合法");
            return res;
        }
        
        if(netId < 0 || netId > 255) {
            res.message("网元ID不合法");
            return res;
        }

        if(!StringHelper.checkIP(ip)){
            res.message("IP地址不合法");
            return res;
        }

        if(slot == null)
            slot = Constants.DefualtNetUnitSlot;
        if(slot <=0 || slot > 20){
            res.message("板卡数目不正确");
            return res;
        }
        
        if(location.length() > 60) {
            res.message("安装位置超过60个字符");
            return res;
        }

        int netUnitId =  netWorkMonitorService.addNetUnit(name, location, netId, ip, Constants.DefualtNetUnitPort, slot);
        if(netUnitId < 0){
            if(netUnitId == -2)
                res.status(Response.STATUS_PARAM_ERROR).message("IP地址与其他网元存在冲突");
            else
                res.status(Response.STATUS_SYS_ERROR);
            return res;
        }
        res.status(Response.STATUS_SUCCESS).data(netUnitId);
        sysLogService.addLog("添加网元" + name);
        return res;
    }

    @RequestMapping(method = RequestMethod.POST, value = "netunit/modify")
    public @ResponseBody
    Response modifyNetUnit(@RequestParam(value="id") int id,
                           @RequestParam(value="name") String name,
                           @RequestParam(value="location") String location,
                           @RequestParam(value="ip") String ip,
                           @RequestParam(value="netId") int netId,
                           @RequestParam(value="slot", required = false) Integer slot){
        Response res = Response.Builder().status(Response.STATUS_PARAM_ERROR);
        if(name.equals("") || name.length() > 60){
            res.message("网元名字不合法");
            return res;
        }

        if(!StringHelper.checkIP(ip)){
            res.message("ip地址不合法");
            return res;
        }

        if(slot == null)
            slot = Constants.DefualtNetUnitSlot;
        if(slot <=0 || slot > 20){
            res.message("板卡数目不正确");
            return res;
        }

        int netUnit = netWorkMonitorService.modifyNetUnit(id, netId, name,ip, Constants.DefualtNetUnitPort, slot, location);
        if(netUnit == -1){
            res.message("网元不存在");
            return res;
        }

        if(netUnit == -2){
            res.message("ip地址与其他网元冲突");
            return res;
        }

        res.status(Response.STATUS_SUCCESS).data(netUnit);
        sysLogService.addLog("修改网元" + name + "配置");
        return res;
    }


    @RequestMapping(method = RequestMethod.POST, value = "netunit/connect/create")
    public @ResponseBody
    Response netUnitconnect(@RequestParam(value="slot") int slot,
                            @RequestParam(value="port") int port,
                           @RequestParam(value="srcId") int srcId,
                            @RequestParam(value = "destId") int destId){
        Response res = Response.Builder().status(Response.STATUS_PARAM_ERROR);
        if( destId <= 0 || srcId <=0 || slot <0 || port <0)
            return res;
        boolean result = netWorkMonitorService.saveNetConnect(srcId,destId,slot,port);
        if(result) {
            res.status(Response.STATUS_SUCCESS);
        }
        return res;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/topo")
    public @ResponseBody
    Response getConfig(){
        return Response.Builder(netWorkMonitorService.getNetUnitAndDevice());
    }


    @RequestMapping(method = RequestMethod.GET, value = "/netunit/cardslot")
    public @ResponseBody
    Response getCardSlot(
            @RequestParam(value="netunit") int netunit,
            @RequestParam(value="type", required = false, defaultValue = "-1") int type){
        return Response.Builder(netWorkMonitorService.getNetUnitCardSlot(netunit,type));
    }

    @RequestMapping(method = RequestMethod.GET, value = "/netunit")
    public @ResponseBody
    Response getNetUnit(@RequestParam(value="netunit", required = false) Integer netunit){
        if(netunit == null || netunit <= 0)
            return Response.Builder(netWorkMonitorService.getAllNetUnit());
        else
            return Response.Builder(netWorkMonitorService.getNetUnit(netunit));
    }

    @RequestMapping(method =RequestMethod.DELETE, value = "/netunit/remove")
    public @ResponseBody
    Response removeNetUnit(@RequestParam(value="id") int netUnitId){
        return netWorkMonitorService.removeNetUnit(netUnitId);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/card")
    public @ResponseBody
    Response getCard(@RequestParam(value="netUnit") int netUnitId){
        return Response.Builder(netWorkMonitorService.getCardByNetUnit(netUnitId));
    }



    @RequestMapping(method = RequestMethod.POST, value = "/card/modify")
    public @ResponseBody
    Response modifyCard(@RequestParam(value="id") int cardId,
                        @RequestParam(value="type") int type){
        return Response.Builder().status(netWorkMonitorService.modifyCard(cardId,type)?
                Response.STATUS_SUCCESS:Response.STATUS_PARAM_ERROR);
    }



    @RequestMapping(method = RequestMethod.GET, value = "/state")
    public @ResponseBody
    Response getNetworkState(@RequestParam(value="netunit", required = false) Integer netunit){
        if(netunit == null)
            return Response.Builder(netWorkStateService.readAllState());
        else
            return Response.Builder(netWorkStateService.getInitState(netunit));
    }
    
    @RequestMapping(method = RequestMethod.POST, value = "/statistic")
    public @ResponseBody
    Response statistic(){
    	
    	Map<String, Object> map = Maps.newHashMap();
    	
    	map.put("usingBusinessCount", businessService.count());
    	
    	map.put("faultNetunitCount", netWorkStateService.faultNetunitCount());
    	
    	map.put("faultDeviceCount", deviceStateService.faultDeviceCount());
    	
    	return Response.Builder(map);
    }


    @RequestMapping(method = RequestMethod.GET, value = "/refresh")
    public @ResponseBody
    Response refresh(@RequestParam(value="netunit", required = false) Integer netunit){
        if(netunit == null || netunit<=0)
            netWorkStateService.refreshAll();
        else
            netWorkStateService.refresh(netunit);
        return Response.Builder();
    }

    @RequestMapping(method = RequestMethod.GET, value = "/card/type")
    public @ResponseBody
    Response getCardType(){
        return Response.Builder(netWorkMonitorService.getCardTypeMap());
    }
}